# Shows how to create a window with a window icon as well
# The programming style used is object oriented
# Also center the window on the screen

import sys
from PySide import QtGui, QtCore

# 1. Create a class called SimpleWindow. 2. which inherits from Qt.QWidget class.
# So we need to call constructors twice. First for the SimpleWindow class and the second one for the inherited class.
class SimpleWindow(QtGui.QWidget):

    # Code to center the application window on screen
    def center(self):
        # Get the rectangle specifying the geometry of the main window
        geomRect = self.frameGeometry()
        # Get the screen resolution of the monitor and from that get the center point
        centerPoint = QtGui.QDesktopWidget().availableGeometry().center()
        # Move the center of the geometry rectangle to the center of the monitor.
        geomRect.moveCenter(centerPoint)
        # Finally move the top-left point of the application window to the top left point of the geometry rectangle.
        # Thus centering the window on the screen
        self.move(geomRect.topLeft())

    # Constructor for the inherited class
    def __init__(self):
        super(SimpleWindow, self).__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle("Window with icon")
        self.setWindowIcon(QtGui.QIcon("samuraicop.jpg"))
        # Code to center the window is placed in the custom center() function
        self.center()

# The startup code is put inside the main method. (Note that it is inline with the class keyword i.e. not indented)
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    ex = SimpleWindow()
    ex.show()
    sys.exit(app.exec_())



